package com.hainatong.shiphelper.login.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;

import com.base.common.model.http.err.ApiException;
import com.base.common.netBeanPackage.BaseResponse;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseFragment;
import com.base.common.viewmodel.BaseViewObserver;
import com.blankj.utilcode.util.StringUtils;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.api.AppConstants;
import com.hainatong.shiphelper.api.PublicValues;
import com.hainatong.shiphelper.databinding.FragmentChangePwdBinding;
import com.hainatong.shiphelper.login.vm.LoginVM;
import com.hainatong.shiphelper.utils.ClickUtil;
import com.jeremyliao.liveeventbus.LiveEventBus;


public class ChangePwdFragment extends BaseFragment<FragmentChangePwdBinding, LoginVM> {
    private boolean isOpenOldPwd = true;

    private boolean isOpenNewPwd = false;


    public static void start(Context context) {
        Intent start = new Intent(context, ChangePwdFragment.class);
        context.startActivity(start);
        ((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }


    @Override
    protected FragmentChangePwdBinding initDataBinding(LayoutInflater inflater, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, R.layout.fragment_change_pwd, container, false);
    }

    @Override
    public void initView() {

        binding.btnSubmit.setOnClickListener(v -> {

            String oldPwd = binding.etOldPwd.getText().toString().trim();
            String newPwd = binding.etNewPwd.getText().toString().trim();
            String newPwd2 = binding.etNewPwd2.getText().toString().trim();

            if (StringUtils.isEmpty(oldPwd)) {
                UIUtils.showToastSafes(R.string.pwd_input);
                return;
            }

            if (StringUtils.isEmpty(newPwd)) {
                UIUtils.showToastSafes(R.string.pwd_input_new);
                return;
            }

            if (StringUtils.isEmpty(newPwd2)) {
                UIUtils.showToastSafes(R.string.pwd_input_new_again);
                return;
            }

            if (!newPwd.equals(newPwd2)) {
                UIUtils.showToastSafes(R.string.pwd_input_different);
                return;
            }

            viewModel.modifyPwd(PublicValues.getUserInfo().getUuid(),oldPwd, newPwd).observe(this, new BaseViewObserver<LiveDataWrapper<BaseResponse>>(getActivity()) {
                @Override
                protected void onSuccess(LiveDataWrapper<BaseResponse> data) {
                    UIUtils.showToastSafes(R.string.pwd_update_success);
                    LiveEventBus.get(AppConstants.PWD_SUCCESS, Integer.class).post(1);
                }

                @Override
                protected void onAppError(ApiException error) {
                    super.onAppError(error);
                    UIUtils.showToastSafesClose(error.getMessage());
                }
            });


        });
        //设置 明文
        binding.etOldPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

        // 隐藏 就密码
        binding.oldPwdShowBtn.setOnClickListener(v -> {
            if (ClickUtil.isFastClick(500, v)) {
                return;
            }

            isOpenOldPwd = !isOpenOldPwd;
            if (isOpenOldPwd) {
                // 明文
                binding.etOldPwd.setTransformationMethod(
                        HideReturnsTransformationMethod.getInstance());
                binding.oldPwdShowBtn.setImageResource(R.mipmap.ic_eye_open);
            } else {
                // 显示密文
                binding.etOldPwd.setTransformationMethod(
                        PasswordTransformationMethod.getInstance());
                binding.oldPwdShowBtn.setImageResource(R.mipmap.ic_eye_close);
            }
        });


        binding.newPwdShowBtn.setOnClickListener(v -> {
            if (ClickUtil.isFastClick(500, v)) {
                return;
            }
            isOpenNewPwd = !isOpenNewPwd;
            if (isOpenNewPwd) {
                // 明文
                binding.etNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                binding.newPwdShowBtn.setImageResource(R.mipmap.ic_eye_open);
            } else {
                // 显示密文
                binding.etNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                binding.newPwdShowBtn.setImageResource(R.mipmap.ic_eye_close);
            }

        });

    }
}
